<template>
	<view>
		<view class="position-fixed p-bottom p-left p-right p-top d-a-j-c flex-column" v-if="list.length==0">
			<view class="wid-40 j-center flex-column">
				<image src="../../static/noadd.png" mode="widthFix" lazy-load></image>
			</view>
			<view class="font-32 black50-zt wid-80 text-center line-h42">您还没有服务地址/收货地址！</view>
		</view>
		<view v-else class="center700">
			<radio-group @change="change">
				<view class="list-item white-bj border-r-15 pad-l-30 pad-r-30 pad-t-30 pad-b-20 mar-top10" v-for="(item,index) in list" :key="index">
					<view>
						<view class="a-center j-sp">
							<view class="font-w-b font-28" @tap="sele(item)">{{item.name}} {{item.tel}}</view>
							<view class="icon-suggestion" @tap="open('edit',item)"></view>
						</view>
					</view>
					<view class="font-24 black50-zt line-h35 mar-top10 border-b-1-91 pad-b-20" @tap="sele(item)">{{item.province}}{{item.city}}{{item.district}}{{item.area}}{{item.address}}{{item.house_number}}</view>
					<view class="a-center j-sp mar-top20">
						<view class="a-center">
							<view class="font-26">
								<radio :value="item.id" :checked="item.isdefault==1" color="#e5353f" style="transform: scale(0.7);"></radio>
								默认地址
							</view>
						</view>
						<view class="main-zt font-26" @tap="del(item.id)">删除</view>
					</view>
				</view>
			</radio-group>
		</view>
		<view class="position-fixed foot d-a-j-c wid-100">
			<view class="center700 white-zt border-r pad-l-40 pad-r-40 line-h80 main-bj text-center box-sizing" @tap="open('add',0)">添加地址</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				type:0,
				checkbox:false,
				longitude:"",
				latitude:"",
				list:[]
			}
		},
		onLoad(e) {
			this.type=parseFloat(e.type)
			this.latitude=this.$user.state.latitude
			this.longitude=this.$user.state.longitude
			this.GetAddress()
			//开启分享
			wx.showShareMenu({
				withShareTicket:true,
				menus:["shareAppMessage","shareTimeline"]
			})
		},
		onShow() {
			this.GetAddress()
		},
		//设置分享好友的内容
		onShareAppMessage(res) {
			if (res.from=="button"){
				console.log(res.target)
			}
			return {
				title:"鑫意到家",
				path: '/pages/index/index?rid=',
				desc:"鑫意到家 · 洁新留下！",
				imageUrl:"../../static/202310131154405365.png"
			}
		},
		//分享朋友圈
		onShareTimeline(res) {
			return {
				title: '鑫意到家 · 洁新留下！',
				type: 0,
				summary: "鑫意到家 · 洁新留下！",
			}
		},
		methods: {
			//选择地址返回上一页
			sele(e){
				if(this.type!=1){return}
				uni.$emit('updateData', e)//传选中的数据给上一级页面
				uni.navigateBack({
					delta: 1
				})
			},
			//打开页面
			open(type,id){
				switch (type){
					case "add":
					uni.navigateTo({
						url:'/pagesB/address/address?latitude='+this.latitude+'&longitude='+this.longitude+'&id='+id
					})
					break;
					case "edit":
					uni.navigateTo({
						url:'/pagesB/address/address?latitude='+id.latitude+'&longitude='+id.longitude+'&id='+id.id
					})
					break;
				}
			},
			//获取用户地址
			async GetAddress(){
				const one={
					aid:38,
					mid:parseFloat(this.$user.state.userId),
					page:1,
					limit:1000
				}
				await this.$api.GetAddressList(one).then(res=>{
					this.list=res.data.list
				})
			},
			//删除地址
			del(id){
				const one={
					aid:38,
					id:id,
					mid:parseFloat(this.$user.state.userId)
				}
				uni.showModal({
					title:'提示',
					content:'确定设置这个地址为默认地址吗？',
					cancelText:"取消",
					confirmText:"确定",
					success: (res) => {
						if (res.confirm) {
							this.$api.AddressDel(one).then(res=>{
								uni.showToast({
									icon:"success",
									title:"删除成功！"
								})
								this.GetAddress()
							})
						}
					}
				})
			},
			//修改默认地址
			change(e){
				let newObj={}
				this.list.forEach(item=>{
					if(item.id==parseFloat(e.detail.value)){
						newObj=item
					}
				})
				const one={
					aid:1,
					id:parseFloat(e.detail.value),
					mid:parseFloat(this.$user.state.userId),
					province:newObj.province,
					city:newObj.city,
					district:newObj.district,
					area:newObj.area,
					latitude:newObj.latitude.toString(),
					longitude:newObj.longitude.toString(),
				}
				uni.showModal({
					title:'提示',
					content:'确定设置这个地址为默认地址吗？',
					cancelText:"取消",
					confirmText:"确定",
					success: (res) => {
						if (res.confirm) {
							this.$api.AddressEdit(one).then(res=>{
								uni.showToast({
									icon:"success",
									title:"设置成功！"
								})
								this.GetAddress()
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	image{
		width: 100%;
		display: block;
	}
	.list-item{
		.icon-suggestion{
			color: #999999;
		}
	}
	.ljm-checkbox{
		width: 30rpx;
		height: 30rpx;
		border: 2rpx solid #999999;
		display: flex;
		align-items: center;
		justify-content: center;
		.icon-duihao{
			font-size: 22rpx;
		}
	}
	.ljm-checkbox.ljm-checkbox-active{
		background-color: #e5353f;
		border: 2rpx solid #e5353f;
	}
	.foot{
		bottom:50rpx;
	}
</style>
